new Vue({
        el: "#app",
        data: {
            userList: [],
            total: 0,
            searchData: {
                nickname: '',
                status: '',
                gender: '',
                graduation_year: '',
                pageNo: 1,
                pageSize: 5,
                sizes: []
            },
            selectedIds: [],
            allSelected: false
        },
        created: function () {
            this.getUserBySearch(1);
        },
        methods: {
            getUserBySearch: function (pageNo) {
                //处理上一页的页码溢出问题
                if (pageNo <= 1) {
                    this.searchData.pageNo = 1;
                } else {
                    this.searchData.pageNo = pageNo;
                }
                //直接调用后台接口
                axios({
                    url: "/pns/getUserBySearch",
                    method: "get",
                    params: this.searchData
                }).then(result => {  //then后面只能跟箭头函数，不能写普通函数
                    let code = result.data.code;
                    if (code == "200") {  //查询成功
                        //将数据绑定到页面
                        this.userList = result.data.data;
                        this.total = result.data.total;
                        //计算页码
                        let chu = parseInt(this.total / this.searchData.pageSize);
                        let yu = this.total % this.searchData.pageSize;
                        if (yu > 0) {
                            chu++;
                        }
                        this.searchData.sizes.length = chu;
                        for (let i = 0; i < chu; i++) {
                            this.searchData.sizes[i] = i + 1;
                        }
                    } else {//查询失败

                    }
                });
            },
            goToAddUser:function () {
                location.href="/pns/adduser.html"
            },
            gotoUpdate:function (user_id) {
                //将user_id传给编辑页面
                window.localStorage.setItem("user_id",user_id);
                //跳转到编辑页面
                window.location.href="/pns/edituser.html"
            },
            // 批量删除选中的用户，该方法可按需删除
            deleteSelected() {
                if (this.selectedIds.length === 0) {
                    alert('请至少选择一条记录');
                    return;
                }

                if (confirm('确定要删除选中的用户吗？')) {
                    // 遍历选中的 IDs，逐个执行删除操作
                    this.selectedIds.forEach(user_id => {
                        axios({
                            url: "/pns/deleteUserById",
                            method: "get",
                            params: { user_id: user_id }
                        }).then(result => {
                            let code = result.data.code;
                            if (code == "200") {
                                alert("删除成功");
                            } else {
                                alert(result.data.msg);
                            }
                        }).catch(error => {
                            alert('删除失败，请稍后重试');
                        });
                    });

                    // 删除成功后刷新用户列表
                    this.getUserBySearch(this.searchData.pageNo);
                    this.selectedIds = [];
                    this.allSelected = false;
                }
            },
            deleteById:function (user_id) {
                let b = window.confirm("您确定要删除该条信息？");
                if (b){
                    axios({
                        url:"/pns/deleteUserById",
                        method:"get",
                        params:{
                            user_id: user_id
                        }
                    }).then(result=>{
                        let code = result.data.code;
                        if (code == "200"){
                            alert("删除成功");
                            location.href="/pns/main.html";
                        }else{
                            alert(result.data.msg);
                        }
                    });
                }
            },
            selectAll: function () {
                if (this.allSelected) {
                    this.selectedIds = this.userList.map(item => item.user_id);
                } else {
                    this.selectedIds = [];
                }
            },
            deleteUserByIds: function () {
                if (this.selectedIds.length === 0) {
                    alert('请至少选择一个用户！');
                    return;
                }
                if (confirm('确定要删除选中的用户吗？')) {
                    axios.post('/pns/deleteUserByIds', {
                        ids: this.selectedIds
                    }).then(response => {
                        if (response.status === 200) {
                            alert('删除成功！');
                            this.getUserBySearch(this.searchData.pageNo);
                        }
                    }).catch(error => {
                        console.error('删除失败：', error);
                        alert('删除失败，请稍后重试！');
                    });
                }
            }
        },
        // filters: {
        //     //三个格式化方法
        //     sexFmt: function (param) {
        //         return param == "0" ? '女' : '男';
        //     },
        //     salFmt: function (param) {f
        //         return '￥ ' + param;
        //     },
        //     professionFmt: function (param) {//1攻城狮、2程序猿、3，码龙
        //         if (param == "1") {
        //             return "攻城狮";
        //         } else if (param == "2") {
        //             return "程序猿";
        //         } else if (param == "3") {
        //             return "码龙";
        //         }
        //     }
        // }
    }
);